<?php
namespace app\common\controller;

use think\Request;

class IndexBase extends YuBase
{


    /**
     * @var array 前置操作方法列表
     */
    protected $beforeActionList = [];

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        // 前置操作方法
        if ($this->beforeActionList)
        {
            foreach ($this->beforeActionList as $method => $options)
            {
                is_numeric($method) ?
                    $this->beforeAction($options) :
                    $this->beforeAction($method, $options);
            }
        }
    }


    /**
     * 前置操作
     * @access protected
     * @param  string $method  前置操作方法名
     * @param  array  $options 调用参数 ['only'=>[...]] 或者 ['except'=>[...]]
     * @return void
     */
    protected function beforeAction($method, $options = [])
    {
        if (isset($options['only']))
        {
            if (is_string($options['only']))
            {
                $options['only'] = explode(',', $options['only']);
            }

            if (!in_array($this->request->action(), $options['only']))
            {
                return;
            }
        }
        elseif (isset($options['except']))
        {
            if (is_string($options['except']))
            {
                $options['except'] = explode(',', $options['except']);
            }
            if (in_array($this->request->action(), $options['except']))
            {
                return;
            }
        }

        call_user_func([$this, $method]);
    }
    
    public function test(){
    	//打印环境 use think\facade\Env;
    	$this->show(Env::get('root_path'));
    	$this->show(Env::get('think_path'));
    	$this->show(Env::get('config_path'));
    	$this->show(Env::get('extend_path'));
    	$this->show(Env::get('module_path'));
    	$this->show(Env::get('app_path'));
    	$this->show(\think\facade\App::version());
    	///打印配置 use think\facade\Config;
    	var_dump(Config::get('template.tpl_replace_string.__STATIC__'));
    	return;
    }

    public function show($data){
    	echo '<br>'.$data;
    }

    protected function checkUidByToken($token){
        $data = [
            'token' => $token,
        ];
        $validate = new \app\common\validate\User;
        if (!$validate->scene('checkToken')->check($data)) {
            $this->redirect('login');
        }
        $uid = $this->getUidByToken($token);
        if ($uid==false){
            $this->redirect('login');
        }else{
            //token设置有效期为30天,连续使用的人，永不过期！！
            \cache($token,$uid,60*60*24*30,'login-token');
            return $uid;
        }
    }

    protected function autoChacheView($viewTem){
        // 指定模板输出
        $viewdata =  $this->view->fetch($viewTem);
        //监听view，并生成缓存
        \think\facade\Hook::listen("view_filter", $viewdata);
        return $viewdata;
    }
}
